package com.sinosoft.dsp.platform.components.portal.output;

import java.util.List;

import com.sinosoft.dsp.platform.components.portal.iteration.SinoAbstractLazyTreeViewBean;
import com.sinosoft.dsp.platform.components.portal.iteration.SinoTreeDataObject;
import com.sinosoft.dsp.platform.services.CodeSetService;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.to.IGenericTO;
import com.sinosoft.dsp.system.to.attribute.Attribute;
import com.sinosoft.dsp.system.to.helper.ToHelper;
import com.sinosoft.dsp.system.utils.Const;

/**
 * 代码集选择树的ViewBean
 * 
 * @author 潘巍（PeterPan）
 * @since 2011-6-26 下午09:18:32
 * 
 */
public class SinoCodeSetSelectTreeViewBean extends
		SinoAbstractLazyTreeViewBean<SinoTreeDataObject> implements
		ISinoCodeSetSelectTreeViewBean {

	public static final String INIT_PARAMETER_CODE_SET_ID = "codeSetId";

	public static final String INIT_PARAMETER_CODE_ID = "codeId";

	public static final String INIT_PARAMETER_FORM_ID = "formId";

	public static final String INIT_PARAMETER_ELEMENT_ID = "elementId";

	private String codeSetId;

	private String codeId;

	private String formId;

	private String elementId;

	@Override
	public void init(IGenericTO tos) throws SinoException {
		Attribute<?> codeSetIdA = ToHelper.getContentAttriubteByName(tos,
				INIT_PARAMETER_CODE_SET_ID);
		Attribute<?> codeIdA = ToHelper.getContentAttriubteByName(tos,
				INIT_PARAMETER_CODE_ID);
		Attribute<?> formIdA = ToHelper.getContentAttriubteByName(tos,
				INIT_PARAMETER_FORM_ID);
		Attribute<?> elementIdA = ToHelper.getContentAttriubteByName(tos,
				INIT_PARAMETER_ELEMENT_ID);

		if (codeSetIdA != null)
			this.codeSetId = codeSetIdA.convertToString();

		if (codeIdA != null)
			this.codeId = codeIdA.convertToString();

		if (formIdA != null)
			this.formId = formIdA.convertToString();

		if (elementIdA != null)
			this.elementId = elementIdA.convertToString();

	}

	@Override
	public List<SinoTreeDataObject> loadFirstLevelNodes() {
		if (!Const.isEmpty(codeSetId)) {
			CodeSetService css = SinoManagedBean.findBean("codeSetService");
			return css.getRootSubCodesTree(codeSetId);
		}
		return null;
	}

	@Override
	public List<SinoTreeDataObject> loadSubNodes(String id) {
		if (!Const.isEmpty(codeSetId) && !Const.isEmpty(id)) {
			CodeSetService css = SinoManagedBean.findBean("codeSetService");
			return css.getSubCodesTree(codeSetId, id);
		}
		return null;
	}

	public String getCodeSetId() {
		return codeSetId;
	}

	public void setCodeSetId(String codeSetId) {
		this.codeSetId = codeSetId;
	}

	public String getCodeId() {
		return codeId;
	}

	public void setCodeId(String codeId) {
		this.codeId = codeId;
	}

	public String getFormId() {
		return formId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public String getElementId() {
		return elementId;
	}

	public void setElementId(String elementId) {
		this.elementId = elementId;
	}

}
